package TCPMain;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 数据类，单实例，共同维护
 */
public class AllData {
    private static AllData instance = null;
    /**
     * 服务器返回的全部数据
     */
    private static JSONObject AllJsonData;
    /**
     * 服务器IP
     */
    private static String SERVER_IP;
    /**
     * 服务器端口
     */
    private static int SERVER_PORT;
    /**
     * 用户名
     */
    private static String username;
    /**
     * 密码
     */
    private static String password;
    /**
     * 文件夹名列表，从服务器数据中提取的文件夹名列表
     */
    private static List<String> FloderNames;
    /**
     * 文件数据列表，从服务器数据中提取的文件数据列表
     */
    private static Map<String, Long> FileDatas;
    /**
     * 文件名字，从文件数据中提取的文件名字列表
     */
    private static List<String> FileNames;
    /**
     * 文件大小，从文件数据中提取的文件大小列表
     */
    private static List<Long> FileSizes;
    /**
     * 文件夹名字和文件名字的结合
     */
    private static List<String> AllNames;

    /**
     * 文件下载的socket映射，key为文件名，value为socket
     *
     * @return
     */
    private static Map<JSONObject, Client> FileClients;

    /**
     * 等待文件下载的列表
     *
     * @return
     */
    private static List<JSONObject> WaitDownList;

    public static synchronized AllData getInstance() {
        if (instance == null) {
            instance = new AllData();
        }
        return instance;
    }

    public static synchronized void setAllJsonData(JSONObject data) throws JSONException {
        AllJsonData = data;
        Log.d("AllData", "setAllJsonData: " + AllJsonData.toString());
        if (AllJsonData.has("SERVER_IP")) {
            SERVER_IP = AllJsonData.getString("SERVER_IP");
        }

        if (AllJsonData.has("SERVER_PORT")) {
            SERVER_PORT = AllJsonData.getInt("SERVER_PORT");
        }

        if (AllJsonData.has("username")) {
            username = AllJsonData.getString("username");
        }

        if (AllJsonData.has("password")) {
            password = AllJsonData.getString("password");
        }

        if (AllJsonData.has("folders")) {
            JSONArray folderNamesArray = AllJsonData.getJSONArray("folders");
            FloderNames = new ArrayList<>();
            for (int i = 0; i < folderNamesArray.length(); i++) {
                FloderNames.add(folderNamesArray.getString(i));
            }
        }

        if (AllJsonData.has("files")) {
            FileNames = new ArrayList<>();
            FileSizes = new ArrayList<>();
            JSONObject fileDatasObject = AllJsonData.getJSONObject("files");
            FileDatas = new HashMap<>();
            Iterator<String> keys = fileDatasObject.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                FileDatas.put(key, fileDatasObject.getLong(key));

                FileNames.add(key);
                FileSizes.add(fileDatasObject.getLong(key));
            }
        }

        AllNames = new ArrayList<>();
        AllNames.addAll(FloderNames);
        AllNames.addAll(FileNames);
    }

    public static synchronized JSONObject getAllJsonData() {
        return AllJsonData;
    }

    public static synchronized void setSERVER_IP(String ip) {
        SERVER_IP = ip;
    }

    public static synchronized String getSERVER_IP() {
        return SERVER_IP;
    }

    public static synchronized void setSERVER_PORT(int port) {
        SERVER_PORT = port;
    }

    public static synchronized int getSERVER_PORT() {
        return SERVER_PORT;
    }

    public static synchronized void setUsername(String username) {
        TCPMain.AllData.username = username;
    }

    public static synchronized String getUsername() {
        return username;
    }

    public static synchronized void setPassword(String password) {
        TCPMain.AllData.password = password;
    }

    public static synchronized String getPassword() {
        return password;
    }

    public static synchronized void setFloderNames(List<String> folderNames) {
        FloderNames = folderNames;
    }

    public static synchronized List<String> getFloderNames() {
        return FloderNames;
    }

    public static synchronized void setFileDatas(Map<String, Long> fileDatas) {
        FileDatas = fileDatas;
    }

    public static synchronized Map<String, Long> getFileDatas() {
        return FileDatas;
    }

    public static synchronized void setFileNames(List<String> fileNames) {
        FileNames = fileNames;
    }

    public static synchronized List<String> getFileNames() {
        return FileNames;
    }

    public static synchronized void setFileSizes(List<Long> fileSizes) {
        FileSizes = fileSizes;
    }

    public static synchronized List<Long> getFileSizes() {
        return FileSizes;
    }

    public static synchronized void setAllNames(List<String> allNames) {
        AllNames = allNames;
    }

    public static synchronized List<String> getAllNames() {
        return AllNames;
    }

    public static synchronized Map<JSONObject, Client> getFileClients() {
        if (FileClients == null) {
            FileClients = new HashMap<>();
        }
        return FileClients;
    }

    public static synchronized void setFileClients(Map<JSONObject, Client> fileClients) {
        FileClients = fileClients;
    }

    public static synchronized void addFileClient(JSONObject filedata, Client client) {
        if (FileClients == null) {
            FileClients = new HashMap<>();
        }
        if(WaitDownList==null){
            WaitDownList=new ArrayList<>();
        }
        FileClients.put(filedata, client);
    }

    public static synchronized void removeFileClient(JSONObject filedata) {
        if (FileClients == null) {
            FileClients = new HashMap<>();
        }
        if(WaitDownList==null){
            WaitDownList=new ArrayList<>();
        }
        if (FileClients.containsKey(filedata)){
            FileClients.remove(filedata);
        }
        else if(WaitDownList.contains(filedata)){
            WaitDownList.remove(filedata);
        }else{
            Log.d("AllData", "removeFileClient 不存在的数据: "+filedata.toString());
        }
    }
    public static synchronized List<JSONObject> getWaitDownList(){
        if(WaitDownList==null){
            WaitDownList=new ArrayList<>();
        }
        return WaitDownList;
    }
    public static synchronized void addWaitDownList(JSONObject filedata){
        if(WaitDownList==null){
            WaitDownList=new ArrayList<>();
        }
        WaitDownList.add(filedata);
    }
    public static synchronized void removeWaitDownList(JSONObject filedata){
        if(WaitDownList==null){
            WaitDownList=new ArrayList<>();
        }
        WaitDownList.remove(filedata);
    }

}
